package com.wang.tank;

import java.util.Vector;

/**
 * @author 王先锋
 * @version 1.0
 */
public class EnemTank extends Tank implements Runnable {
    //在敌人坦克类，使用Vector保存多个shot
    Vector<Shot> shots = new Vector<>();
    //增加成员，EnemyTank可以得到敌人坦克的Vector
    //
    Vector<EnemTank> enemyTanks = new Vector<>();
    boolean isLive = true;


    public void setEnemyTanks(Vector<EnemTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public EnemTank(int x, int y) {
        super(x, y);
    }


    public boolean isTouchEnemyTank() {
//判断当前敌人坦克方向
        switch (this.getDirect()) {
            case 0://上
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从vector中取出一个敌人坦克
                    EnemTank enemTank = enemyTanks.get(i);
                    //不和自己比较
                    if (enemTank != this) {
                        //如果敌人坦克
                        if (enemTank.getDirect() == 0 || enemTank.getDirect() == 2) {
                            //如果敌人坦克方向是上下
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 40
                                    && this.getY() >= enemTank.getY()
                                    && enemTank.getY() <= enemTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemTank.getX()
                                    && this.getX() + 40 <= enemTank.getX() + 40
                                    && this.getY() >= enemTank.getY()
                                    && enemTank.getY() <= enemTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemTank.getDirect() == 1 || enemTank.getDirect() == 3) {
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 60
                                    && this.getY() >= enemTank.getY()
                                    && this.getY() <= enemTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemTank.getX()
                                    && this.getX() + 40 <= enemTank.getX() + 60
                                    && this.getY() >= enemTank.getY()
                                    && this.getY() <= enemTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;
            case 1://右
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从vector中取出一个敌人坦克
                    EnemTank enemTank = enemyTanks.get(i);
                    //不和自己比较
                    if (enemTank != this) {
                        //如果敌人坦克
                        if (enemTank.getDirect() == 0 || enemTank.getDirect() == 2) {
                            //如果敌人坦克方向是上下
                            if (this.getX() + 60 >= enemTank.getX()
                                    && this.getX() + 60 <= enemTank.getX() + 40
                                    && this.getY() >= enemTank.getY()
                                    && enemTank.getY() <= enemTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemTank.getX()
                                    && this.getX() + 60 <= enemTank.getX() + 40
                                    && this.getY() + 40 >= enemTank.getY()
                                    && enemTank.getY() + 40 <= enemTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemTank.getDirect() == 1 || enemTank.getDirect() == 3) {
                            if (this.getX() + 60 >= enemTank.getX()
                                    && this.getX() + 60 <= enemTank.getX() + 60
                                    && this.getY() >= enemTank.getY()
                                    && this.getY() <= enemTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 60 >= enemTank.getX()
                                    && this.getX() + 60 <= enemTank.getX() + 60
                                    && this.getY() + 40 >= enemTank.getY()
                                    && this.getY() + 40 <= enemTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;
            case 2://下
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从vector中取出一个敌人坦克
                    EnemTank enemTank = enemyTanks.get(i);
                    //不和自己比较
                    if (enemTank != this) {
                        //如果敌人坦克
                        if (enemTank.getDirect() == 0 || enemTank.getDirect() == 2) {
                            //如果敌人坦克方向是上下
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 40
                                    && this.getY() + 60 >= enemTank.getY()
                                    && enemTank.getY() + 60 <= enemTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemTank.getX()
                                    && this.getX() + 40 <= enemTank.getX() + 40
                                    && this.getY() + 60 >= enemTank.getY()
                                    && enemTank.getY() + 60 <= enemTank.getY() + 60) {
                                return true;
                            }
                        }
                        if (enemTank.getDirect() == 1 || enemTank.getDirect() == 3) {
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 60
                                    && this.getY() + 60 >= enemTank.getY()
                                    && enemTank.getY() + 60 <= enemTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() + 40 >= enemTank.getX()
                                    && this.getX() + 40 <= enemTank.getX() + 60
                                    && this.getY() + 60 >= enemTank.getY()
                                    && enemTank.getY() + 60 <= enemTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;
            case 3://左
                for (int i = 0; i < enemyTanks.size(); i++) {
                    //从vector中取出一个敌人坦克
                    EnemTank enemTank = enemyTanks.get(i);
                    //不和自己比较
                    if (enemTank != this) {
                        //如果敌人坦克
                        if (enemTank.getDirect() == 0 || enemTank.getDirect() == 2) {
                            //如果敌人坦克方向是上下
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 40
                                    && this.getY() >= enemTank.getY()
                                    && enemTank.getY() <= enemTank.getY() + 60) {
                                return true;
                            }
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 40
                                    && this.getY() + 40 >= enemTank.getY()
                                    && enemTank.getY() + 40 <= enemTank.getY() + 40) {
                                return true;
                            }
                        }
                        if (enemTank.getDirect() == 1 || enemTank.getDirect() == 3) {
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 60
                                    && this.getY() >= enemTank.getY()
                                    && this.getY() <= enemTank.getY() + 40) {
                                return true;
                            }
                            if (this.getX() >= enemTank.getX()
                                    && this.getX() <= enemTank.getX() + 60
                                    && this.getY() + 40 >= enemTank.getY()
                                    && this.getY() + 40 <= enemTank.getY() + 40) {
                                return true;
                            }
                        }
                    }

                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        while (true) {
            Shot s = null;
            //这里我们判断如果shots.size()=0
            if (isLive && shots.size() < 3) {
                switch (getDirect()) {
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        s = new Shot(getX() + 20, getY() + 60, 0);
                        break;
                    case 3:
                        s = new Shot(getX(), getY() + 20, 0);
                        break;

                }
                shots.add(s);
                new Thread(s).start();
            }

            //根据坦克的方向来继续移动
            switch (getDirect()) {
                case 0://向上
                    for (int i = 0; i < 10; i++) {
                        if (getY() > 0 && !isTouchEnemyTank())
                            moveUp();
                    }
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;

                case 1://向右
                    for (int i = 0; i < 10; i++) {
                        if (getX() + 60 < 1000&& !isTouchEnemyTank()) {
                            moveRight();
                        }
                    }
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2://向下
                    for (int i = 0; i < 10; i++) {
                        if (getY() + 60 < 750&& !isTouchEnemyTank()) {
                            moveDown();
                        }
                    }
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                case 3://向左
                    for (int i = 0; i < 10; i++) {
                        if (getX() > 0&& !isTouchEnemyTank())
                            moveLeft();
                    }
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;

            }


            //随机改变坦克方向
            setDirect((int) (Math.random() * 4));
            //改线程什么时候结束
            if (!isLive) {
                break;//退出线程
            }
        }
    }
}
